/***************************************************************************
 创建者: hualei
 开始时间: 2018-10-20
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018-10-15 类的具体实现代码编写

 ***************************************************************************
 *  TCP_START 以太网通信建立指令具体实现
 ***************************************************************************/
#include "tcpstartinstructionlogic.h"
#include "programset.h"
#include "programengine.h"
#include "tcpmanager.h"
#include "tcpstartinstruction.h"
#include "GeneralDefine.h"
#include "messagelog.h"

extern TcpManager *tcpManager;

TcpStartInstructionLogic::TcpStartInstructionLogic(int robotIdIn, ProgramSet *programSetIn, ProgramEngine *programEngineIn)
{

    robotId = robotIdIn;
    programSet = programSetIn;
    programEngine = programEngineIn;
}

E_PROGRAM_LOOP_RESULT TcpStartInstructionLogic::startTcpStart(int programNumIn, const TcpStartInstruction &instructionIn, int lineIndex)
{
<<<<<<< HEAD
    tcpManager->setTcpRobotId(robotId);
=======
    tcpManager->programEngine = programEngine;
    tcpManager->robotId = robotId;
>>>>>>> origin/master

    // 1-判断指令类型是否有效
    if(instructionIn.instructionType != TCP_START)
    {
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    if(instructionIn.tcpStartBusType == E_TCP_BUSTYPE_CLIENT)
    {
        QString ip = "";
        for(int i = 0; i < instructionIn.tcpStartServerIPVec.count(); i++)
        {
            if(i == 3)
            {
                ip += QString::number(instructionIn.tcpStartServerIPVec[i]);
            }
            else
            {
                ip += QString::number(instructionIn.tcpStartServerIPVec[i]) + ".";
            }
        }
        qDebug() << "TCP_START ip:" << ip;
        qDebug() << "TCP_START prot:" << instructionIn.tcpStartPort;

        if(1!= tcpManager->tcpStart_client(ip, instructionIn.tcpStartPort) )
        {

            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine",  3910, robotId
                    , programEngine->getRunningLineFloat());
            QString tmpjumpLabel=QString::number(instructionIn.jmpLabelValue);
            return programEngine->startJump(programNumIn, tmpjumpLabel);
//            return E_PROGRAM_LOOP_RESULT_NORMAL;
        }
    }
    else if(instructionIn.tcpStartBusType == E_TCP_BUSTYPE_SERVER)
    {
        QString ip = "";
        for(int i = 0; i < instructionIn.tcpStartClientIPVec.count(); i++)
        {
            if(i == 3)
            {
                ip += instructionIn.tcpStartClientIPVec[i];
            }
            ip += instructionIn.tcpStartClientIPVec[i] + ".";
        }

        if(1!= tcpManager->tcpStart_server(ip,instructionIn.tcpStartPort) )
        {

            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine",  3911, robotId
                    , programEngine->getRunningLineFloat());
            QString tmpjumpLabel=QString::number(instructionIn.jmpLabelValue);
            return programEngine->startJump(programNumIn, tmpjumpLabel);
//            return E_PROGRAM_LOOP_RESULT_NORMAL;
        }
    }

    programEngine->calculateNextLineToRun(programNumIn);
    return programEngine->isTheLastInstruction(programNumIn);

}

void TcpStartInstructionLogic::addMsg(int messageLevel, string componentName, string messageType, int messageCode, int robotId, int parameter1, int parameter2, int parameter3, int parameter4, QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;

    QString infomationStr;

    switch(messageCode)
    {
    case 3911:
    {

       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常（创建TCP服务端失败）！")+message;

        break;
    }
    case 3910:
    {

       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常（创建TCP客户端失败）！")+message;

        break;
    }
        default:
        {

        }
    }

    tmpMsg.MessageInformation = infomationStr.toStdString();
    MessageLog::getInstance()->addMessage(tmpMsg);
}
